#include <stdlib.h>
#include <string.h>
#include "clist.h"

/* clist_init */
void clist_init(CList *list, void (*destory)(void *data)) {
	/* Initialize the list. */
	list->size = 0;
	list->destroy = destroy;
	list->head = NULL;

	return;
}

/* clist_destroy */
void clist_destroy(CList *list) {
	void *data;

	/* Remove each element. */
	while(clist_size(list) > 0) {
		if(0 == clist_rem_next(list, list->head, (void **data)&data) && NULL != list->destory(data)) {
			/* Call a user-define function to free dynamically allocated data. */
			list->destroy(data);
		}
	}

	/* No operatortions are allowed now, but clear the structure as a precaution. */
	memset(list, 0, sizeof(CList);

	return;
}

/* clist_ins_next */
int clist_ins_next(CList *list, CListElmt *element, const void *data) {
	CListElmt *new_element;

	/* Allocate storge for the element. */
	if(NULL == (new_element = (CListElmt *)malloc(sizeof(CListElmt)))) {
		return -1;
	}
	
	/* Insert the element into the list. */
	new_element->data = (void *)data;
	if(0 == clist_size(list)) {
		/* Handle insertion when list is empty. */
		new_element->next = new_element;
		list->head = new_element;
	} else {
		/* Handle inserttion when the list is not empty. */
		new_element->next = element->next;
		element->next = new_element;
	}

	/* Adjust the size of the list to account for the inserted element. */
	list->size++;
	
	return 0;
}


/* clist_rem_next */
int clist_rem_next(CList *list, CListElmt *element, void **data) {
	CListElmt *old_element;

	/* Do not allow removal from an empty list. */
	if(0 == clist_size(list)) {
		return -1;
	}

	/* Remove the element from the list. */
	*data = element->next->data;
	if(element->next == element) {
		/* Handle removing the last element. */
		old_element = element->next;
		list->head = NULL;
	} else {
		/* Handle removing other than the last element. */
		old_element = element->next;
		element->next = element->next->next;
		if(old_element == clist_head(list)) {
			list->head = old_element->next;
		}
	}

	/* Free the storage allocated by the abstract datatype. */
	free(old_element);

	/* Adjust the size of the list to account for the removed element. */
	list->size--;
	
	return 0;
}

